package com.corpgovernment.organization.service.impl;

import com.corpgovernment.common.apollo.OrganizationApollo;
import com.corpgovernment.organization.entity.db.BdFpFfcRelation;
import com.corpgovernment.organization.entity.db.MbOrgUserFfpinfo;
import com.corpgovernment.organization.entity.db.MbPassportWarnInfo;
import com.corpgovernment.organization.mapper.BdFpFfcRelationMapper;
import com.corpgovernment.organization.mapper.MbOrgUserFfpMapper;
import com.corpgovernment.organization.mapper.MbPassportWarnInfoMapper;
import com.corpgovernment.organization.service.IOrganizationUserFfpService;
import com.corpgovernment.organization.service.OrganizationBaseService;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户常旅客信息serviceImpl
 *
 * @author wangyujue
 */
@Service
@Slf4j
public class OrganizationUserFfpServiceImpl extends OrganizationBaseService implements IOrganizationUserFfpService {

    @Autowired
    private MbOrgUserFfpMapper mbOrgUserFfpMapper;
    @Autowired
    private BdFpFfcRelationMapper bdFpFfcRelationMapper;
    @Autowired
    private MbPassportWarnInfoMapper mbPassportWarnInfoMapper;
    @Autowired
    private OrganizationApollo organizationApollo;

    @Override
    public List<MbOrgUserFfpinfo> listByUid(String uid) {

        if (StringUtils.isEmpty(uid)) {
            return new ArrayList<>();
        }

        MbOrgUserFfpinfo selectParam = new MbOrgUserFfpinfo();
        selectParam.setUid(uid);
        selectParam.setIsDeleted(false);
        return mbOrgUserFfpMapper.select(selectParam);
    }

    @Override
    public boolean save(MbOrgUserFfpinfo mbOrgUserFfpinfo) {
        int result = mbOrgUserFfpMapper.insertSelective(mbOrgUserFfpinfo);
//        insertOperateLog(mbOrgUserFfpinfo);
        return result > 0;
    }

    @Override
    public boolean update(MbOrgUserFfpinfo mbOrgUserFfpinfo) {

        int result = mbOrgUserFfpMapper.updateByPrimaryKeySelective(mbOrgUserFfpinfo);
        return result > 0;
    }

    @Override
    public boolean delete(Long id) {
        MbOrgUserFfpinfo mbOrgUserFfpinfo = new MbOrgUserFfpinfo();
        mbOrgUserFfpinfo.setId(id);
        int result = mbOrgUserFfpMapper.delete(mbOrgUserFfpinfo);
        deleteOperateLog(String.valueOf(id));
        return result > 0;
    }

    @Override
    public boolean deleteByUid(String uid) {
        mbOrgUserFfpMapper.deleteByUid(uid);
        deleteOperateLog(uid);
        return false;
    }

    @Override
    public boolean deleteByNonEmployeeUid(Long nonEmployeeUid) {
        return mbOrgUserFfpMapper.deleteByNonEmployeeUid(nonEmployeeUid) > 0;
    }

    @Override
    public List<MbOrgUserFfpinfo> listByUids(List<String> uids) {
        return mbOrgUserFfpMapper.listByUids(uids);
    }

    @Override
    public List<MbOrgUserFfpinfo> listByNonEmployeeIds(List<Long> uids) {
        return mbOrgUserFfpMapper.listByNonEmployeeIds(uids);
    }

    @Override
    public boolean selectFfpInfo(String uid, String airline, String ffpno, Long nonEmployeeId) {
        MbOrgUserFfpinfo query = new MbOrgUserFfpinfo();
        query.setIsDeleted(false);
        query.setUid(uid);
        query.setAirline(airline);
        query.setFfpno(ffpno);
        query.setNonEmployeeUid(nonEmployeeId);
        List<MbOrgUserFfpinfo> list = mbOrgUserFfpMapper.select(query);
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean saveList(List<MbOrgUserFfpinfo> mbOrgUserFfpinfoList) {
        int result = mbOrgUserFfpMapper.insertList(mbOrgUserFfpinfoList);
        return result > 0;
    }

    @Override
    public List<BdFpFfcRelation> getAirlineNameByAirlineCode (List<String> airlineCodes) {
        Example example = new Example(BdFpFfcRelation.class);
        example.createCriteria().andIn("cardType", airlineCodes);
        return bdFpFfcRelationMapper.selectByExample(example);
    }

    @Override
    public boolean insertWarnInfo(MbPassportWarnInfo passportWarnInfo) {
        return mbPassportWarnInfoMapper.insertSelective(passportWarnInfo) > 0;
    }

    @Override
    public boolean getNameConfirmSwitch() {
        return Boolean.parseBoolean(organizationApollo.getNameConfirmSwitch());
    }
}
